package org.jeecg.modules.online.cgreport.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.online.cgreport.entity.OnlCgreportItem;
import org.jeecg.modules.online.cgreport.service.IOnlCgreportItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.extern.slf4j.Slf4j;

/**
 * @Title: Controller
 * @Description: 在线报表配置
 * @Author: jeecg-boot
 * @Date: 2019-03-08
 * @Version: V1.0
 */
@RestController("onlCgreportItemController")
@RequestMapping("/online/cgreport/item")
@Slf4j
public class OnlCgreportItemController {

	@Autowired
	private IOnlCgreportItemService onlCgreportItemService;

	/**
	 * 根据 headId 查询出 所有的 item
	 */
	@GetMapping(value = "/listByHeadId")
	public Result<?> queryPageListByHeadId(@RequestParam("headId") String headId) {
		QueryWrapper<OnlCgreportItem> queryWrapper = new QueryWrapper<OnlCgreportItem>();
		queryWrapper.eq("cgrhead_id", headId);
		queryWrapper.orderByAsc("order_num");
		List<OnlCgreportItem> list = onlCgreportItemService.list(queryWrapper);

		Result<List<OnlCgreportItem>> result = new Result<List<OnlCgreportItem>>();
		result.setSuccess(true);
		result.setResult(list);
		return result;
	}

	/**
	 * 分页列表查询
	 * 
	 * @param onlCgreportItem
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/list")
	public Result<IPage<OnlCgreportItem>> queryPageList(OnlCgreportItem onlCgreportItem, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
		Result<IPage<OnlCgreportItem>> result = new Result<IPage<OnlCgreportItem>>();
		QueryWrapper<OnlCgreportItem> queryWrapper = QueryGenerator.initQueryWrapper(onlCgreportItem, req.getParameterMap());
		Page<OnlCgreportItem> page = new Page<OnlCgreportItem>(pageNo, pageSize);
		IPage<OnlCgreportItem> pageList = onlCgreportItemService.page(page, queryWrapper);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}

	/**
	 * 添加
	 * 
	 * @param onlCgreportItem
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody OnlCgreportItem onlCgreportItem) {
		onlCgreportItemService.save(onlCgreportItem);
		return Result.ok("添加成功!");
	}

	/**
	 * 编辑
	 * 
	 * @param onlCgreportItem
	 * @return
	 */
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody OnlCgreportItem onlCgreportItem) {
		onlCgreportItemService.updateById(onlCgreportItem);
		return Result.ok("编辑成功!");
	}

	/**
	 * 通过id删除
	 * 
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
		onlCgreportItemService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 * 
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
		this.onlCgreportItemService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}

	/**
	 * 通过id查询
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<OnlCgreportItem> queryById(@RequestParam(name = "id", required = true) String id) {
		Result<OnlCgreportItem> result = new Result<OnlCgreportItem>();
		OnlCgreportItem onlCgreportItem = onlCgreportItemService.getById(id);
		result.setResult(onlCgreportItem);
		result.setSuccess(true);
		return result;
	}

}
